package com.midian.action.mc.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.midian.action.mc.common.AjaxAction;
import com.midian.common.annotation.FunctionAnnotation;
import com.midian.common.exception.BizException;
import com.midian.common.system.SysStatic;
import com.midian.model.user.User;
import com.midian.model.user.vo.UserInfoVo;
import com.midian.service.user.IUserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 用户Action
 * 
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("userAction")
public class UserAction extends AjaxAction<User> {

	private static final long serialVersionUID = 966106926621215688L;

	/** 日志对象 */
	private final Logger LOG = Logger.getLogger(UserAction.class);

	/** 用户对象 */
	private User entity;
	/** 用户对象列表 */
	private List<User> rows;
	/** 用户业务 */
	private IUserService userService;

	/** 用户输入的验证码 */
	private String code;
	/** 用户所绑定的角色id列表 */
	private String roleIdStr;
	/**帐号校验唯一性flag*/
	private Integer flag;

	
	
	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public User getEntity() {
		return entity;
	}

	public void setEntity(User entity) {
		this.entity = entity;
	}

	@Override
	protected User dytEntity() {
		if (this.entity == null) {
			this.entity = new User();
		}
		return getEntity();
	}

	@Override
	protected void dyEntity(User t) {
		setEntity(t);
	}

	public List<User> getRows() {
		return rows;
	}

	public void setRows(List<User> rows) {
		this.rows = rows;
	}

	@Resource(name = "userService")
	public void setUserService(IUserService userService) {
		this.service = userService;
		this.userService = userService;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getRoleIdStr() {
		return roleIdStr;
	}

	public void setRoleIdStr(String roleIdStr) {
		this.roleIdStr = roleIdStr;
	}

	@Override
	public String ajax() {
		return super.ajax();
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	public String login() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String code = String.valueOf(session.get("code")); // 获取图片中的验证码
		try {
			if (StringUtils.isBlank(this.code) || !this.code.equals(code)) {
				throw new BizException("验证码不正确!");
			}
			session.remove("code");
			// 登录
			UserInfoVo userInfoVo = this.userService.login(this.entity);
			session.put(SysStatic.MC_LOGIN, userInfoVo); // 设置用户VO到session中
		} catch (BizException e) {
			session.put("loginName", this.entity.getAccount());
			session.put("errorMessage", e.getMessage());
			return LOGIN;
		}
		return INDEX;
	}

	/**
	 * 登出
	 * 
	 * @return
	 */
	public String logout() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.remove(SysStatic.MC_LOGIN);
		session.clear();
		return LOGIN;
	}

	/**
	 * 初始化用户对象属性
	 * 
	 * @param userType
	 *            用户类型，1：管理员，2：会员，3：销售员
	 */
	private void initUserProps(Integer userType) {
		String id = dytEntity().getId();
		if (StringUtils.isBlank(id)) {// 新增
			// 用户属性初始化
			this.entity.setUserfrom(IUserService.USER_FROM_NORMAL);
			this.entity.setDelFlag(SysStatic.NORMALDELFLAG);

			if (IUserService.IDENTITY_MANAGER.equals(userType)) {
				// 管理员，设置身份为管理员
				this.entity.setIdentity(IUserService.IDENTITY_MANAGER);
			} else if (IUserService.IDENTITY_ELSE.equals(userType)) {
				// 会员，设置身份为会员、设置为非管理员
				this.entity.setIdentity(IUserService.IDENTITY_ELSE);
				this.entity.setIsAdmin(SysStatic.IS_NOT_ADMIN);
				this.entity.setPassword("123456"); // 会员设置初始密码为123456
			}
		}
		/*
		 * public String name() {
		 * 
		 * }
		 */
	}
	
	
	@Override
	@FunctionAnnotation(funAuth = "操作员管理")
	public String saveInfo() {
		initUserProps(IUserService.IDENTITY_MANAGER);
//		// 用户属性初始化
		this.entity.setUserfrom(IUserService.USER_FROM_NORMAL);
//		this.entity.setDelFlag(SysStatic.NORMALDELFLAG);
		// 选择超级管理员的情况
		if (SysStatic.NORMALDELFLAG.equals(this.entity.getIsAdmin())) {
			this.roleIdStr = "";
		}
		if (StringUtils.isNotBlank(this.roleIdStr)) {
			String[] roleIdArr = this.roleIdStr.split(","); // 角色id拼接字符串
			// 保存角色id列表
			List<String> roleIdList = new ArrayList<String>();
			for (String roleId : roleIdArr) {
				roleIdList.add(roleId);
			}
			this.entity.setRoleIdList(roleIdList);
		}
		
		return super.saveInfo();
	}
	
	/**
	 * 保存会员对象
	 * @return
	 */
	@FunctionAnnotation(funAuth = "会员管理")
	public String saveMemberInfo() {
		initUserProps(IUserService.IDENTITY_ELSE);
		return super.saveInfo();
	}
	
//	/**
//	 * 保存销售员对象
//	 * @return
//	 */
//	@FunctionAnnotation(funAuth = "销售员管理")
//	public String saveSaleManInfo() {
//		initUserProps(IUserService.IDENTITY_SALESMAN);
//		return super.saveInfo();
//	}

	@Override
//	@FunctionAnnotation(funAuth = "操作员管理")
	public String showInfo() {
		try {
			if (this.id != null) {
				// 根据用户id，获取用户的角色id列表
				List<String> roleIdList = this.userService.getRoleIdListByUserId(this.id);
				// 设置用户的角色id拼接字符串
				this.roleIdStr = StringUtils.join(roleIdList, ",");
			}
		} catch (BizException e) {
			LOG.error(e.getMessage());
		}
		return super.showInfo();
	}

	@Override
	@FunctionAnnotation(funAuth = "操作员管理")
	public String showList() {
		if (this.entity == null) {
			this.entity = new User();
		}
//		this.entity.setIsAdmin(SysStatic.IS_NOT_ADMIN);
//		this.entity.setDelFlag(SysStatic.NORMALDELFLAG);
		this.entity.setIdentity(IUserService.IDENTITY_MANAGER);
		//只筛选出用户来源为1的数据
		this.entity.setUserfrom(1);
		return super.showList();
	}

	/**
	 * 获取会员列表
	 * 
	 * @return
	 */
	@FunctionAnnotation(funAuth = "会员管理")
	public String showMemberList() {
		if (this.entity == null) {
			this.entity = new User();
		}
		this.entity.setDelFlag(null);
		this.entity.setIsAdmin(SysStatic.IS_NOT_ADMIN);
		this.entity.setIdentity(IUserService.IDENTITY_ELSE);
		try {
			this.rows = this.userService.showList(this.entity, this.pager, super.getOrder());
		} catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}

//	/**
//	 * 获取销售员列表
//	 * @return
//	 */
//	@FunctionAnnotation(funAuth = "销售员管理")
//	public String showSalesManList() {
//		if(this.entity == null) {
//			this.entity = new User();
//		}
//		this.entity.setIsAdmin(SysStatic.DELFLAG);
//		this.entity.setIdentity(IUserService.IDENTITY_SALESMAN);
//		try {
//			this.rows = this.userService.showList(this.entity, this.pager, super.getOrder());
//		} catch (BizException e) {
//			getError(e);
//		}
//		return SUCCESS;
//	}
	
	/**
	 * 校验帐号唯一性
	 * @return
	 */
	public String onlyAccount(){
		if(this.entity.getAccount()!=null){
			try {
				User userTemp = this.userService.getUserByAccount(this.entity.getAccount(),this.id);
				if(userTemp==null){
					this.flag=1;//帐号不存在
				}else{
					this.flag=0;//帐号存在
				}
			} catch (BizException e) {
				getError(e);
			}
		}
		return SUCCESS;
	}
	
	
	
}
